package com.shuoyue.ycgk.ui.recruitment.resume;

import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.Resume;
import com.shuoyue.ycgk.entity.address.AddressBean;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.exception.NetErrorException;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;
import com.shuoyue.ycgk.views.dialog.addresspicker.AddressPickerDialog;

import java.util.List;

import io.reactivex.Observable;


public interface MyResumeContract {
    interface View extends BaseView {
        void getResumeSuc(Resume resume);

        void setAreas(List<AddressBean> areas, int pid, int type, AddressPickerDialog addressPickerDialog);

        void saveSuc();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void saveResume(Resume resume) {
            apply(model.saveResume(resume))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> recruitmentMainInfoOptional) {
                            super.onNext(recruitmentMainInfoOptional);
                            mView.saveSuc();
                        }


                    });
        }

        public void getResume() {
            apply(model.getResume())
                    .subscribe(new ApiSubscriber<Optional<Resume>>(mView, this) {
                        @Override
                        public void onNext(Optional<Resume> recruitmentMainInfoOptional) {
                            super.onNext(recruitmentMainInfoOptional);
                            mView.getResumeSuc(recruitmentMainInfoOptional.getIncludeNull());
                        }
                    });
        }

        public void getArea(int pid, int level, AddressPickerDialog addressPickerDialog) {
            apply(model.getArea(pid))
                    .subscribe(new ApiSubscriber<Optional<List<AddressBean>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<AddressBean>> listOptional) {
                            super.onNext(listOptional);
                            if (listOptional.getIncludeNull().size() == 0) {
                                mView.onError(new NetErrorException("暂无地址数据", 499));
                            } else
                                mView.setAreas(listOptional.getIncludeNull(), pid, level, addressPickerDialog);
                        }
                    });
        }
    }

    class Model {
        public Observable<BaseResult<String>> saveResume(Resume resume) {
            return RetrofitClient.getInstance().getApi().saveResume(resume);
        }

        public Observable<BaseResult<Resume>> getResume() {
            return RetrofitClient.getInstance().getApi().getResume();
        }

        public Observable<BaseResult<List<AddressBean>>> getArea(int pid) {
            return RetrofitClient.getInstance().getApi().getSysRegionList(pid);
        }
    }
}
